package com.jlastu.znb.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.jlastu.znb.R;


public abstract class BaseFragment extends Fragment {
    protected final String TAG = this.getClass().getName();
    protected View mView;
    public Context mContext;
    private TextView mTextView1;
    private TextView mTextView2;
    private TextView mTextView3;
    private TextView mTextView4;

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initListener();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), container, false);

        initView(); // 初始化视图和引用
        initListener();

        // 调用setTabSelected设置默认选中的标签
        mTextView1 = mView.findViewById(R.id.toolbar_title1);
        mTextView2 = mView.findViewById(R.id.toolbar_title2);
        mTextView3 = mView.findViewById(R.id.toolbar_title3);
        mTextView4 = mView.findViewById(R.id.toolbar_title4);
        setTabSelected(mTextView1);

        return mView;
    }

    protected void onCreateFragmentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView();
        initListener();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    //基类中添加通用方法
    public void showToast(String message) {
        if(mContext != null && message != null) {
            Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
        }
    }
    private void setTabSelected(TextView selectedTab) {
        if (selectedTab == null) {
            // 处理 selectedTab 为 null 的情况，可能是记录日志或抛出异常
            return;
        }

        // 重置所有标签为未选中状态
        if (mTextView1 != null) mTextView1.setSelected(false);
        if (mTextView2 != null) mTextView2.setSelected(false);
        if (mTextView3 != null) mTextView3.setSelected(false);
        if (mTextView4 != null) mTextView4.setSelected(false);

        // 将被点击的标签设置为选中状态
        selectedTab.setSelected(true);
    }
}
